Elasticsearch Java 入门教程之索引管理常用功能 Demo (二)

您所在的位置:网站首页 java操作elasticsearch 教程 Elasticsearch Java 入门教程之索引管理常用功能 Demo (二)

Elasticsearch Java 入门教程之索引管理常用功能 Demo (二)

2024-07-17 17:50| 来源: 网络整理| 查看: 265

本教程系列目录:

入门教程 Demo (一)入门教程之索引管理常用功能 Demo (二)入门教程之文档管理常用功能 Demo (三)入门教程之搜索常用功能 Demo (四)入门教程之聚合常用功能 Demo (五)

Github源码:https://github.com/Mengzuozhu/es-demo

IndexService 索引管理常用功能示例:配置(setting)、映射(mapping)、索引(index)管理、结构与数据复制等示例

package com.mzz.esdemo.service; import com.alibaba.fastjson.JSONObject; import com.mzz.esdemo.common.constant.EsConstant; import com.mzz.esdemo.common.util.JsonUtil; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryRequest; import org.elasticsearch.index.reindex.ReindexRequest; import org.springframework.stereotype.Service; import java.util.List; /** * The type Document service. * See * * @author Zero */ @Service @RequiredArgsConstructor public class DocumentService { private final RestHighLevelClient restHighLevelClient; /** * Create doc. * * @param index the index * @param id the id * @param source the source * @return the doc write response */ @SneakyThrows public DocWriteResponse createDoc(String index, String id, Object source) { IndexRequest indexRequest = new IndexRequest(index) .id(id) .source(JsonUtil.toJsonString(source), XContentType.JSON); return restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); } /** * Upsert doc. * * @param index the index * @param id the id * @param source the source * @return the doc write response */ @SneakyThrows public DocWriteResponse upsertDoc(String index, String id, Object source) { String jsonString = JsonUtil.toJsonString(source); UpdateRequest updateRequest = new UpdateRequest(index, id) .doc(jsonString, XContentType.JSON) .upsert(jsonString, XContentType.JSON); return restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); } /** * Upsert doc by bulk. * * @param index the index * @param sources the sources * @return the bulk response */ @SneakyThrows public BulkResponse upsertDocByBulk(String index, List sources) { BulkRequest bulkRequest = new BulkRequest(index); for (JSONObject source : sources) { String jsonString = JsonUtil.toJsonString(source); UpdateRequest updateRequest = new UpdateRequest(index, source.getString(EsConstant.ID)) .doc(jsonString, XContentType.JSON) .upsert(jsonString, XContentType.JSON); bulkRequest.add(updateRequest); } return restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); } /** * Gets doc. * * @param index the index * @param id the id * @return the doc */ @SneakyThrows public GetResponse getDoc(String index, String id) { GetRequest deleteRequest = new GetRequest(index, id); return restHighLevelClient.get(deleteRequest, RequestOptions.DEFAULT); } /** * Delete doc. * * @param index the index * @param id the id * @return the doc write response */ @SneakyThrows public DocWriteResponse deleteDoc(String index, String id) { DeleteRequest deleteRequest = new DeleteRequest(index, id); return restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); } /** * Delete by query. * * @param index the index * @param queryJson the query json * @return the bulk by scroll response */ @SneakyThrows public BulkByScrollResponse deleteByQuery(String index, String queryJson) { DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(index) .setQuery(QueryBuilders.wrapperQuery(queryJson)); return restHighLevelClient.deleteByQuery(deleteRequest, RequestOptions.DEFAULT); } /** * Clear index. * * @param index the index * @return the bulk by scroll response */ @SneakyThrows public BulkByScrollResponse clearIndex(String index) { return deleteByQuery(index, QueryBuilders.matchAllQuery().toString()); } /** * Reindex. * * @param sourceIndex the source index * @param destIndex the dest index * @return the bulk by scroll response */ @SneakyThrows public BulkByScrollResponse reindex(String sourceIndex, String destIndex) { return restHighLevelClient.reindex(new ReindexRequest() .setSourceIndices(sourceIndex) .setDestIndex(destIndex), RequestOptions.DEFAULT); } }

测试类



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3